using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;
using Northwoods.Go;
using Northwoods.Go.Xml;

namespace Workflows.Diagrams
{
    [Serializable()]
    public class TransformActivityNode : GoXmlTransformer
    {
        public TransformActivityNode()
        {
            this.TransformerType = typeof(ActivityNode);
            this.ElementName = "activity";
            this.IdAttributeUsedForSharedObjects = true;
            this.BodyConsumesChildElements = true;
        }

        public override void GenerateDefinitions(object obj)
        {
            base.GenerateDefinitions(obj);
            ActivityNode n = (ActivityNode)obj;
            foreach (IGoPort p in n.Ports)
            {
                this.Writer.DefineObject(p.GoObject);
            }
        }

        public override void GenerateAttributes(object obj)
        {
            base.GenerateAttributes(obj);
            ActivityNode n = (ActivityNode)obj;
            this.WriteAttrVal("type", (int)n.ActivityType);
            this.WriteAttrVal("xy", n.Icon.Position);
            this.WriteAttrVal("size", n.Icon.Size);
            this.WriteAttrVal("label", n.Text);
            if (!n.LabelOffset.Equals(new SizeF(-99999, -99999)))
                this.WriteAttrVal("labelOffset", n.LabelOffset);
            if (n.UserObject.GetType().IsSubclassOf(typeof(Components.WorkflowComponentBase)))
            {
                Components.WorkflowComponentBase wfComponent = (Components.WorkflowComponentBase)n.UserObject;
                // TODO: implement serialization of wf component
            }
        }

        public override void GenerateBody(object obj)
        {
            base.GenerateBody(obj);
            ActivityNode n = (ActivityNode)obj;
            foreach (IGoPort p in n.Ports)
            {
                this.Writer.GenerateObject(p.GoObject);
            }
        }

        public override object Allocate()
        {
            ActivityNode n = new ActivityNode();
            n.Initialize(null, "workflow.gif", "");
            return n;
        }

        public override void ConsumeAttributes(object obj)
        {
            base.ConsumeAttributes(obj);
            ActivityNode n = (ActivityNode)obj;
            // this.SetIconImageBasedOnUserObject(n);
            n.Icon.Position = PointFAttr("xy", new PointF(100, 100));
            if (this.IsAttrPresent("size"))
                n.Icon.Size = SizeFAttr("size", n.Icon.Size);
            n.Text = StringAttr("label", n.Text);
            if (this.IsAttrPresent("labelOffset"))
                n.LabelOffset = SizeFAttr("labelOffset", n.LabelOffset);
        }

        //private void SetIconImageBasedOnUserObject(ActivityNode node)
        //{
        //    if (node != null)
        //    {
        //        if (node.UserObject.GetType().IsSubclassOf(typeof(Components.WorkflowComponentBase)))
        //        {

        //        }
        //    }
        //}

        public override void ConsumeChild(object parent, object child)
        {
            base.ConsumeChild(parent, child);
            ActivityNode n = (ActivityNode)parent;
            n.Add(child);
        }
    }
}
